//
//  ByteTool.m
//  BalanceCar
//
//  Created by 郑信鸿 on 2017/8/24.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "ByteTool.h"

@implementation ByteTool


//截取byte[]中一部分数据，从begin 开始，长度是 count
- (void)bytesplit2byte:(Byte[])src orc:(Byte[])orc begin:(NSInteger)begin count:(NSInteger)count{
    memset(orc, 0, sizeof(char)*count);
    for (NSInteger i = begin; i < begin+count; i++){
        orc[i-begin] = src[i];
    }
}


//把 byte数组转成 Int类型的数据，这 提供了一种 存储思想
-(int)lBytesToInt:(Byte[]) byte
{
    int height = 0;
    NSData * testData =[NSData dataWithBytes:byte length:4];
    for (int i = 0; i < [testData length]; i++)
    {
        if (byte[[testData length]-i] >= 0)
        {
            height = height + byte[[testData length]-i];
        } else
        {
            height = height + 256 + byte[[testData length]-i];
        }
        height = height * 256;
    }
    if (byte[0] >= 0)
    {
        height = height + byte[0];
    } else {
        height = height + 256 + byte[0];
    }
    return height;
}


- (void)test{
    
    int jsonlen = 1;
    char *p_json = (char *)&jsonlen;
    char str_json[4] = {0};
    
    for(int i= 0 ;i < 4 ;i++)
        
    {
        str_json[i] = *p_json;
        p_json ++;
    }
    
}

+ (void)byteToBits:(Byte[])bits byte:(Byte)byte{
    
    for (int i = 0; i < 8; i++) {
        
        bits[i] = byte >> i & 0x1;
        
    }
}



+ (void)hexString:(NSString *)hexString pwbytes:(Byte[])bytes{
    
    NSUInteger length = hexString.length;
    
    for (int i = 0; i < length; i++) {
        
        NSString *byteString = [hexString substringWithRange:NSMakeRange(i, 1)];
        bytes[i] = [self numberWithHexString:byteString];
        
    }
    
}

+ (Byte)numberWithHexString:(NSString *)hexString{
    
    const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
    
    int hexNumber;
    
    sscanf(hexChar, "%x", &hexNumber);
    
    return (Byte)hexNumber;
}


@end
